﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;

namespace TestGame {
    /// <summary>
    /// Used in helping to manage exceptions thrown during runtime. It is vital
    /// for users to know what exceptions are thrown, so that the game does not
    /// simply crash to desktop without reason.
    /// </summary>
    class ErrorHelper {
        /// <summary>
        /// Handles the given exception by comparing it to any known or
        /// expected exceptions. If it is an expected exception, the Method
        /// will handle it accordingly. If it is an unexpected exception, the 
        /// Method will dump the error to a file, and create a GameScreen 
        /// summarizing the error.
        /// </summary>
        /// <param name="error">The exception to handle</param>
        /// <param name="handler">An event handler for the Screen to use or null</param>
        /// <returns></returns>
        public static GameScreen HandleError(Exception error, EventHandler<PlayerIndexEventArgs> handler) {
            string text = "Unexpected Error has occured.";

            DumpExceptionToFile(error);

            MessageBoxScreen errorScreen = new MessageBoxScreen(text, false);

            if (handler != null) {
                errorScreen.Accepted += handler;
                errorScreen.Cancelled += handler;
            }

            return errorScreen;
        }

        /// <summary>
        /// Handles the given exception by comparing it to any known or
        /// expected exceptions. If it is an expected exception, the Method
        /// will handle it accordingly. If it is an unexpected exception, the 
        /// Method will dump the error to a file, and create a GameScreen 
        /// summarizing the error.
        /// </summary>
        /// <param name="socketError">The exception to handle</param>
        /// <param name="handler">An event handler for the Screen to use or null</param>
        /// <returns></returns>
        public static GameScreen HandleError(SocketException socketError, EventHandler<PlayerIndexEventArgs> handler) {
            SocketError errorType = socketError.SocketErrorCode;
            string text = errorType.ToString();

            if (socketError.SocketErrorCode == SocketError.ConnectionReset) {
                text = "Other player has disconnected.";
            }
            else {
                DumpExceptionToFile(socketError);
                text = "Unexpected SocketError has occured.";
            }

            MessageBoxScreen errorScreen = new MessageBoxScreen(text, false);

            if (handler != null) {
                errorScreen.Accepted += handler;
                errorScreen.Cancelled += handler;
            }

            return errorScreen;
        }

        /// <summary>
        /// Dumps the Exception to a file. The file's name is based off the 
        /// current date and time. 
        /// </summary>
        /// <param name="error"></param>
        static void DumpExceptionToFile(Exception error) {
            try {
                String outputText = String.Format("Date & Time: {0}\n", System.DateTime.Now.ToString());
                outputText += String.Format("HelpLink: {0}\n", error.HelpLink);
                outputText += String.Format("Message: {0}\n", error.Message);
                outputText += String.Format("Source: {0}\n", error.Source);
                outputText += String.Format("Data: {0}\n", error.Data);
                outputText += String.Format("StackTrace: {0}\n", error.StackTrace);
                outputText += String.Format("TargetSite: {0}\n", error.TargetSite);

                string path = Directory.GetCurrentDirectory();
                string file = path + "\\" + System.DateTime.Now.ToFileTimeUtc().ToString() + ".txt";
                Console.WriteLine("Writing to {0}", file);
                File.WriteAllText(file, outputText);
            }
            catch { }
        }
    }
}
